package org.mics.lang;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串工具
 * @author mics
 * @date 2022-4-19
 * @version 1.0
 */
public class StringsUtils {
	/**
	 * 空白字符
	 */
	private static final Pattern WHITE_PATTERN = Pattern.compile("\\s*|\t|\r|\n");
	
	/**
	 * 旧身份证长度
	 */
	private static final int OLD_IDCARD_LENGTH = 15;
	/**
	 * 新身份证长度
	 */
	private static final int NEW_IDCARD_LENGTH = 18;
	/**
	 * 护照
	 */
	private static final int PASS_PORT_LENGTH=9;
	/**
	 * 移除html标签
	 * @author mics
	 * @date 2022-4-19
	 * @version 1.0
	 */
	public static String removeHtml(String text) {
		if(text != null) {
			text = text.replaceAll("<[^>]*>", "").replaceAll("[(/>)<]", "").replaceAll("&ldquo;", "\"")
					.replaceAll("&rdquo;", "\"").replaceAll("&nbsp;", "");
		}
		return text;
	}
	
	/**
	 * 手机号脱密
	 * @author mics
	 * @date 2022-5-24
	 * @version  1.0
	 * @param content 脱密内容 
	 * @return 返回保留前三位和后三位，中间用*替换
	 */
	public static String desensitizationPhone(String content) {
		 content = content.replaceAll("(\\w{3})\\w*(\\w{3})", "$1*****$2");
		 return content;
	}
	
	/**
	 * 身份证脱密
	 * @author mics
	 * @date 2022-5-24
	 * @version  1.0
	 * @param content 脱密内容 
	 * @return 返回保留前三位和后三位，中间用*替换
	 */
	public static String desensitizationIdCard(String content) {
		if(content.length() == OLD_IDCARD_LENGTH) {
			 content = content.replaceAll("(\\w{6})\\w*(\\w{4})", "$1*****$2");
		}else if(content.length() == NEW_IDCARD_LENGTH){
			 content = content.replaceAll("(\\w{6})\\w*(\\w{4})", "$1********$2");
		}else if(content.length() == PASS_PORT_LENGTH){
			 content = content.replaceAll("(\\w{2})\\w*(\\w{3})", "$1****$2");
		}
		 return content;
	}
	
	/**
	 * 移除空字符串和换行符
	 * @author mics
	 * @date 2022-7-5
	 * @version  1.0
	 * @param content 需要移除空白字符串
	 * @return 移除之后的字符串
	 */
	public static String removeBlank(String content) {
		if(content != null) {
			//去除空字符串
			content = content.replace(" ", "");
			//去除换行
			Matcher m = WHITE_PATTERN.matcher(content);
			content = m.replaceAll("");
		}
		return content;
	}
	
	public static void main(String[] args) {
		System.out.println(removeBlank(" "));
	}
	
	
	
}
